<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>体检检查</title>
</head>
<body>
<script type="text/javascript">
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var opts = $(this).datagrid('options');
			var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
					
				}
			}
			$(this).datagrid('beginEdit', param.index);
			
			for(var i=0; i<fields.length; i++){
				var col = $(this).datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});

var editIndex = undefined;
function endEditing(){
	if (editIndex == undefined){return true}
	if ($('#phy-healpro-table').datagrid('validateRow', editIndex)){
		$('#phy-healpro-table').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}
function onClickCell(index, field){
	if (endEditing()){
		$('#phy-healpro-table').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
		editIndex = index;
	}
}



	$("#checkupall-plane").tabs({
		onSelect: function(title,index){
			if(title=="自主检查"){
 		    	  $("#heal1").show(); 
 		     }else{
 		    	 $("#heal1").hide(); 
 		     }
			
		}
	});
	
	//体检项目
	$('#phy-healpro-table').datagrid({
	    url:'',
	    columns:[[	   
	    	    {field:'project_no',title:'编号',width:100},
	    	 	{field:'project_name',title:'项目名称',width:120},
				{field:'project_price',title:'项目价格',width:100},
				{field:'health_result',title:'体检结果',editor:'text',width:180}
	    ]]
	}); 
	
	//自主体检的表格
	$("#personhealproject-tab").datagrid({
	    url:'init-appoint',
	    onLoadSuccess: function (data) { 
			if(data==null||data.total ==0){
				$(this).datagrid('appendRow',{
					        appo_no:'<div style="height:50px;line-height:50px;font-size:22px;text-align:center;color:red;">没有相关记录！</div>',
							rownumbers : false
					//合并单元格
				}).datagrid('mergeCells',{
							index : 0,
							field : 'appo_no',
							colspan : 5
				})
				//隐藏分页导航条
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
			}else{
				$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
			}

		}, 
	    columns:[[
	    	    {field:'appo_no',title:'预约编号',width:88},
	    	    {field:'clinic_id',title:'医疗卡主键',width:160,hidden:true},
	    	 	{field:'clinic_name',title:'姓名',width:98},
				{field:'clinic_sex',title:'性别',width:95},
	    	 	{field:'clinic_age',title:'年龄',width:95},
	    	 	{field:'appo_people',title:'办理人',width:85}
	       ]],
	       onDblClickRow : function(rowIndex, rowData){
	    		//赋值给医疗主键
		   		$("#id").textbox('setValue',rowData.clinic_id);
		   		//赋值给病人
		   		$("#sname").textbox('setValue',rowData.clinic_name);
		   		//只能读不能编辑
		   		$("#id").textbox('textbox').attr('readonly',true);  
		   		$("#sname").textbox('textbox').attr('readonly',true);
		   	    //体检项目
		   		$('#phy-healpro-table').datagrid({
		   		    url:'init-appointdetails',
		   		    onClickCell:onClickCell,
		   		    queryParams:{
		   		    	clinicid:rowData.clinic_id
		   		    }, 
		   		    columns:[[	   
		   		    	    {field:'project_no',title:'编号',width:100},
		   		    	 	{field:'project_name',title:'项目名称',width:120},
		   					{field:'project_price',title:'项目价格',width:100},
		   					{field:'health_result',title:'体检结果',editor:'text',width:180}
		   		    	]]
		   		});
		   		
		   		$('#phy-healpro-table').datagrid('clearChecked');
	       }
	});

	
	//从门诊住院体检的信息
	$("#paitenthospitalheal-tab").datagrid({
		    url:'init-healthtwo',
		    onLoadSuccess: function (data) { 
				if(data==null||data.total ==0){
					$(this).datagrid('appendRow',{
						        health_no:'<div style="height:50px;line-height:50px;font-size:22px;text-align:center;color:red;">没有相关记录！</div>',
								rownumbers : false
						//合并单元格
					}).datagrid('mergeCells',{
								index : 0,
								field : 'health_no',
								colspan : 5
					})
					//隐藏分页导航条
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
				}else{
					$(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
				}

			}, 
		    columns:[[
		    	    {field:'health_no',title:'体检编号',width:85},
		    	    {field:'clinic_id',title:'门诊医疗卡主键',hidden:true},
		    	    {field:'lookillness_id',title:'看诊号',hidden:true},
		    	    {field:'healthtype',title:'类型',width:98,
   					   formatter:function(value,row,index){
	   						if(row.lookillness_id==null){
				                return "住院体检";
							}else if(row.lookillness_id !=null){
								 return "门诊体检";
							}
   					   }
	   				},
		    	 	{field:'clinic_name',title:'姓名',width:95},
					{field:'clinic_sex',title:'性别',width:85},
		    	 	{field:'clinic_age',title:'年龄',width:85}
			   ]],
		       onDblClickRow : function(rowIndex, rowData){
			   		//赋值给医疗主键
			   		$("#id").textbox('setValue',rowData.clinic_id);
			   		//赋值给病人
			   		$("#sname").textbox('setValue',rowData.clinic_name);
			   		//只能读不能编辑
			   		$("#id").textbox('textbox').attr('readonly',true);  
			   		$("#sname").textbox('textbox').attr('readonly',true);
			   		//体检项目
			   		$('#phy-healpro-table').datagrid({
			   		    url:'init-healthdetailstwo',
			   		    onClickCell:onClickCell,
			   		    queryParams:{
			   		    	clinicid:rowData.clinic_id
			   		    },
			   		    columns:[[
			   		            {field:'health_no',title:'体检编号',width:70},
			   		    	    {field:'project_no',title:'编号',width:100},
			   		    	 	{field:'project_name',title:'项目名称',width:120},
			   					{field:'project_price',title:'项目价格',width:60},
			   					{field:'health_result',title:'体检结果',editor:'text',width:180}
			   		    	]]
			   		 });
		   		
			   		$('#phy-healpro-table').datagrid('clearChecked');
		       }
		});
	

	
	
	//确定体检的按钮[自主体检检查]
	$("#add-ownhealth").click(function(){
		//获取体检项目的编号
		var prows = $('#phy-healpro-table').datagrid("getRows");
		//项目编号的数组
		var parr = [];
	    var id =  $("#id").val(); //医疗卡
	    var tab=$('.ttabs').tabs('getSelected');
	    var title=tab.panel('options').title;
		if(title=="自主检查"){
  		    var hid =  $("#healthid").val();//流水体检号
 		    //项目的总押金
			for(var i=0;i<prows.length;i++){
				var obj={};
				obj.project_no =  prows[i].project_no;
				obj.health_result =  prows[i].health_result;
				obj.cid =  parseInt(id);
				obj.Health_no = parseInt(hid);
				parr.push(obj);
				
			}
  		    var json = JSON.stringify(parr);
			$.ajax({
				url:'add-ownhealths',
				method:'Post',
				data:{'json':json},
				Type:"json",
				success:function(data){
					var  rows = $('#phy-healpro-table').datagrid("getRows");
					if(rows){
						for (var i = rows.length-1; i >= 0; i--) {
							var index = $('#phy-healpro-table').datagrid('getRowIndex',rows[i]);
							$('#phy-healpro-table').datagrid('deleteRow',index);
						}
					}
					
				    //刷新表格
			    	$("#personhealproject-tab").datagrid('reload');
			    	$('#phy-heal-report-table').datagrid('reload');
			    	$.messager.show({
                        title:'提示信息' , 
                        msg:'体检成功，请及时查看体检报告!'
                    });
				}
			});
  		}
  		if(title=="门诊住院检查"){
	    	  for(var i=0;i<prows.length;i++){
				var obj={};
				obj.project_no =  prows[i].project_no;
				obj.health_result =   prows[i].health_result;
				obj.cid =  parseInt(id);
				obj.Health_no = prows[i].health_no;
				parr.push(obj);
	 		 }
	  		var json = JSON.stringify(parr);
			$.ajax({
				url:'add-ownhealths',
				method:'Post',
				Type:'json',
				data: {'json':json},
				success:function(data){
					var  rows = $('#phy-healpro-table').datagrid("getRows");
					if(rows){
						for (var i = rows.length-1; i >= 0; i--) {
							var index = $('#phy-healpro-table').datagrid('getRowIndex',rows[i]);
							$('#phy-healpro-table').datagrid('deleteRow',index);
						}
					}
					//刷新表格
			    	$("#paitenthospitalheal-tab").datagrid('reload');
			    	$('#phy-heal-report-table').datagrid('reload');

					$.messager.show({
                        title:'提示信息' , 
                        msg:'体检成功，请及时查看体检报告!'
                    });
					
				    
				}
			});
	  	}

	});

	
	//体检报告
	$('#phy-heal-report-table').datagrid({
		    url:'init-healthpro',
		    onLoadSuccess: function (data) { 
		    	var start = 0;
				var end = 0;
				var mer = ['clinic_name','clinic_sex','clinic_age'];//需要合并的字段，即field
				//有数据就合并----对应数据的数量
				if (data.total > 0){
				    var temp = data.rows[0].clinic_name;   //需要比较的列-----排班类型----门诊还是住院
				    for (var i = 1; i < data.rows.length; i++) {
					    //第一列的门诊或住院类型=遍历过程中的门诊或住院
						if (temp == data.rows[i].clinic_name) {
						     end++;
						} else {
						    if (end> start) {
								for(var j = 0; j < mer.length; j ++){
									$("#phy-heal-report-table").datagrid('mergeCells', {
										index: start,//起始索引
										rowspan: end - start + 1,  //需要合并的行数
										field: mer[j]
									});
								}
						   }
						    temp = data.rows[i].clinic_name;
						    start = i;
						    end = i;
					 }
				 }
				    //这里是为了判断重复内容出现在最后的情况
				    if (end > start) {
						for(var j = 0; j < mer.length; j ++){
							$("#phy-heal-report-table").datagrid('mergeCells', {
								index: start,
								rowspan: end - start + 1,
								field: mer[j]
							});
						}
				    }
				}/* 最大if结束 */

			}, 
		    pagination:true,
		    pageNumber:1,
		    pageSize:7,
		    pageList:[7,14,21],
		    columns:[[
		    	    {field:'health_no',title:'体检单号',width:130},
		    	 	{field:'clinic_name',title:'姓名',width:130},
		    	 	{field:'clinic_sex',title:'性别',width:130},
		    	 	{field:'clinic_age',title:'年龄',width:95},
		    	 	{field:'project_name',title:'项目名称',width:140},
		    	 	{field:'health_result',title:'体检结果',width:130},
					{field:'project_price',title:'项目价格',width:130},
		    	 	{field:'health_times',formatter:comFormatDate,title:'体检时间',width:160}
		    	 	
		    	]]
		});
	
	/**
	 * 时间格式YYY-mm-dd hh:mm:ss
	 */
	function comFormatDate(date) {
		var day = new Date(date); 
		var Year = 0; 
		var Month = 0; 
		var Day = 0; 
		var CurrentDate = ""; 
		//初始化时间 
		Year= day.getFullYear();
		Month= day.getMonth()+1; 
		Day = day.getDate(); 
		Hour = day.getHours(); 
		 Minute = day.getMinutes(); 
		 Second = day.getSeconds(); 
		CurrentDate += Year + "-"; 
		if (Month >= 10 ) { 
			CurrentDate += Month + "-"; 
		}else { 
			CurrentDate += "0" + Month + "-"; 
		} 
		if (Day >= 10 ) { 
			CurrentDate += Day ; 
		}else { 
			CurrentDate += "0" + Day ; 
		} 
		if(Hour>=10){
			CurrentDate +=" "+Hour;
		}else{
			CurrentDate +=" "+"0"+Hour;
		}
		if(Minute>=10){
			CurrentDate +=":"+Minute;
		}else{
			CurrentDate +=":"+"0"+Minute;
		}
		if(Second>=10){
			CurrentDate +=":"+Second;
		}else{
			CurrentDate +=":"+"0"+Second;
		}
		return CurrentDate; 
	}
</script>
	<div id="healcheck-tt" class="easyui-tabs">
	  <div title="体检" style="padding:10px;display:none;">
		   <fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:480px;height:430px;margin-left:20px;float:left">
				<legend style="font-size:15px">病人信息</legend>
				<div id="checkupall-plane" class="ttabs easyui-tabs" style="width:480px;height:300px;">
				    <div title="自主检查" style="padding:10px;display:none;">
			            <table id="personhealproject-tab" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:true,singleSelect:false,width:465" >
					    </table>
    	            </div>
    	            <div title="门诊住院检查" style="padding:10px;display:none;">
			    	    <table id="paitenthospitalheal-tab" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:true,singleSelect:false,width:465" >
					    </table>
			       </div>
	 	      </div>    
		    </fieldset>
		    		
		    <fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:550px;height:430px;margin-left:10px;float:left">
				<legend>体检检查</legend>
		    	<p>
		    	   <a id="heal1">
		               	<span style="margin-right:7px;">体检号</span>
						<input id="healthid" name="healthNo" value="${healthid }" class="easyui-textbox"  data-options="width:80,height:30,required:true">
		             </a>
		             <a id="app" style="display: none;">
		               	<span style="margin-right:7px;">预约号</span>
						<input id="appointid" name="appoNo" class="easyui-textbox"  data-options="width:80,height:30,required:true">
		             </a>
		             <a style="margin-left: 20px;">
		               	<span style="margin-right:7px;">医疗卡号</span>
						<input id="id" name="" class="easyui-textbox"  data-options="width:80,height:30,required:true">
		             </a>
		             <a style="margin-left: 20px;">
		               	<span style="margin-right:7px;">姓名</span>
						<input id="sname" name="" class="easyui-textbox"  data-options="width:80,height:30">
		             </a>
		             <a id="add-ownhealth" class="easyui-linkbutton" style="margin-left:10px;width:90px;" data-options="iconCls:'icon-ok',border:false">保存</a> 
		    	</p>
		    	  
				  <table id="phy-healpro-table" style="height:330px;" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:false,width:502">
				 </table>			 
			</fieldset>
    	</div>
	    <div title="体检报告" style="padding:10px;display:none;">
    		<fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:1050px;height:450px;margin:0 auto;margin-top:10px;margin-bottom:20px;">
				<legend>体检报告</legend>
				<table id="phy-heal-report-table" style="height:400px;" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:false,width:1050">
				</table>
			</fieldset>	  	
       </div>
    </div>
    
    <!-- 弹框提示 
    <div id="warminghealth-win" class="easyui-window"  title="提示框" style="width:300px;height:156px"
    data-options="iconCls:'icon-save',modal:true,resizable:false,collapsible:false,maximizable:false,minimizable:false,closed:true">
          <p class="window-health" style=" margin-left: 72px;font-size: 16px;margin-top: 47px;">
          </p>
    </div>-->
    
</body>
</html>